<?php
/*
 * @Descripttion: 基于thinkPhp-image的image类封装的图片上传类
 * @version: v1
 * @Author: Terry
 * @Date: 2020-12-19 18:34:52
 * @LastEditors: Terry
 * @LastEditTime: 2024-03-08 22:59:09
 */

namespace libs;
use think\Image as thinkImage;

class Image extends thinkImage{

    public $path;
    /**
     * @Descripttion:  实例化对象时传入地址，判断地址是否存在
     * @name: __construct
     * @Author: Terry
     * @Date: 2020-12-19 18:36:17
     * @param {*}
     */
    public function __construct($path=''){
        $this->path = $this->url2path($path);
    }

    /**
     * @Descripttion: 本地图片转base64 
     * @name: localImage2base64
     * @Author: Terry
     * @Date: 2021-03-11 21:44:52
     * @param {*} $file 图片地址
     */
    public function localImage2base64(){
        if($fp = fopen($this->path,"rb", 0)){
            $gambar = fread($fp,filesize($this->path));
            return  base64_encode($gambar);
        }    
        return false;
    }
    
     /**
    * [base642image base64图片数据流转图片]
    * @param  [type] $base64 [ase64图片数据]
    * @param  [type] $path [图片地址]
    * @param  [type] $imagename [图片名字]
    * @return [type]         [description]
    */
    public function base642image($base64,$imagename=''){
        if(strpos($base64,'base64,')) $base64 = substr($base64, strpos($base64, ",") + 1);
        if(!isBase64($base64)) return false;//'传入的不是base64数据';
        if(!$this->createDir()) return false;//'文件不存在或生成失败';
        if (empty($imagename)) $imagename = rand(1111,9999)."_".time()."_".rand(1111,9999).'.jpg';
        $imageSrc= $this->path."/". $imagename; //图片名字
        $r = file_put_contents($imageSrc, base64_decode($base64));//返回的是字节数
        if($r ==0 ) return false;
        // $thumbImage = $this->thumbImage($imageSrc);
        return $imageSrc;
    }

     /**
    * [foshanImage base64图片数据流转图片]
    * @param  [type] $base64 [ase64图片数据]
    * @param  [type] $imagename [图片名字]
    * @return [type]         [description]
    */
    public function foshanImage($base64,$imagename){
        if(!$this->createDir()) return false;//'文件不存在或生成失败';
        $imageSrc= $this->path."/". $imagename; //图片名字
        $r = file_put_contents($imageSrc, base64_decode($base64));//返回的是字节数
        if($r ==0 ) return false;
        return $imageSrc;
    }

   /**
    * @description: 图片压缩
    * @Author: Terry
    * @Date: 2022-05-13 17:30:03
    * @param {*} $path
    */   
   public function thumbImage($path){
        $image = thinkImage::open($path); //要加水印的图片
        //图片宽/高，缩小倍数
        $width = $image->width();
        $height = $image->height($width, $height,self::THUMB_FIXED);
        $image->thumbImage();
   }
   /**
    * @Descripttion: 二进制流数据转图片
    * @name: binary2image
    * @Author: Terry
    * @Date: 2021-03-11 21:45:30
    * @param {*} $binary 二进制数据
    * @param {*} $filename  文件名
    */
   public  function binary2image($binary,$filename){
        if($this->createDir()) return '文件不存在或生成失败';

        $path = $this->path.'/'.$name;

        if (!file_exists('/'.$path)) {
            $fopen  =  fopen($path, 'wb');//新建文件命令
            fclose($fopen);
        }
        $res = file_put_contents($path,$image,FILE_APPEND | LOCK_EX);//保存的数据
        
        if($res) return true;
        return false;
        
   }

   /**
    * @Descripttion: 图片底部添加文字
    * @name: imgButtomAddText
    * @Author: Terry
    * @Date: 2021-04-11 23:54:10
    * @param {*} $path 图片地址
    * @param {*} $text 文字 换行用 /n 隔开
    */
   public function imgButtomAddText($path,$text){
        $exploded = explode('.com/',$path);
        $imgPath = end($exploded);
        $text = explode('\n',$text);
        $image = image::open('assets/image/qrcode-bg.jpg'); //要加水印的图片
        //图片宽/高，缩小倍数
        $image->water($imgPath,2)->save($imgPath);
        // $image->water($imgPath,2)->save('image/test.jpg');
        $width = $image->width();
        $offset = -70;
        foreach($text as $key=>$value){
            $strLen = iconv_strlen($value,'UTF8');
            $fontSize = $width  / 1.5 / $strLen > 30 ? 30 : $width  / 1.5 / $strLen;
            $image->text($value,'assets/front/AlibabaSansHK-55.ttf',$fontSize,'#000000',8,[0,$offset])->save($imgPath);
            // $image->text($value,'assets/front/simkai.ttf',$fontSize,'#000000',8,[0,$offset])->save('image/test.jpg');
            $offset += 50; 
        }
        return $imgPath;
   }

   private function url2path($path){
        $exploded = explode('.com/',$path);
        $imgPath = end($exploded);
        return $imgPath;
   }

   private function createDir(){
       if(!is_dir($this->path)){
            $res= mkdir($this->path,0777,true);
            chmod($this->path,0777);
            return $res;
        }
        return true;
    }

    /**
     * @Description: 在线图片上传之oss
     * @Author: Terry
     * @Date: 2022-06-14 15:52:56
     * @param {*} $img
     */    
    public static function base2oss($img){
        $dir = app()->getRootpath();
        $sdk = new Image($dir . 'public/image/'.date('Ymd'));
        $res = $sdk->base642image($image);
        $oss = new ALIOss('image/'.date('Ymd').'/'.createUuid(),'jpg');
        $image = $oss->upload($res);
        return $image.'?x-oss-process=style/water';
    }


    /**
     * @Description: base2Image
     * @Author: Terry
     * @Date: 2022-06-14 17:38:30
     * @param {*} $base 图片base64编码
     * @param {*} $path 保存的地址
     */    
    public static function base2Image($base,$path){
        if(self::makeDir($path)){
            $imageSrc= $path."/". createUuid().'.jpg'; //图片名字
            $r = file_put_contents($imageSrc, base64_decode($base));//返回的是字节数
            if($r) return $imageSrc;
            return false;
        }
    }

    /**
     * @Description: 创建文件夹
     * @Author: Terry
     * @Date: 2022-06-14 17:36:29
     * @param {*} $path
     */    
    public static function makeDir($dir){
        if (!is_dir($dir)) {
            if (!mkdir($dir,0777,true)) {
                return false; 
            }
            chmod($dir,0777);
        }
        return true;
    }

    /**
     * @Description: 在线图片转base64
     * @Author: Terry
     * @Date: 2022-06-09 15:15:41
     * @param {*} $img 图片地址
     * @param {*} $maxSize 图片不能大于多少KB
     */ 
    public static function onlineImage2base($img,$maxSize='99999999'){
            $img = @file_get_contents($img);
            if(!$img) return false;
            $img = base64_encode($img); 
            $imgLen = strlen($img);
            $size = intval($imgLen - ($imgLen/8)*2);
            $size = number_format(($size/1024),2);//KB
            $data = ['base64'=>$img,'check'=>true];
            if($size > $maxSize) $data['check'] = false;
            return $data;
    }

    /**
     * @Description: png图片转换为jpeg格式
     * @Author: Terry
     * @Date: 2022-08-11 14:25:31
     */    
    public static function png2jpeg($img){
        $imgData = getimagesize($img);
        if($imgData['mime'] == 'image/png'){
            $png = imagecreatefrompng($img);
            $res = imagejpeg($png, $img, 75);
            imagedestroy($png);
        }
        return $img;
    }
}